//
//  WYCFastCspController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/12/26.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCFastCspController.h"
#import "WYCBillModel.h"
#import "WYCCardlistPickerView.h"
//#import "WYCHTMLController.h"

@interface WYCFastCspController ()
{
    UIButton * cardbutton;
    
    NSString * str;
    UIButton * daybutton;
    
    UILabel * backLabel;
    
}
/** 数组*/
@property(nonatomic,strong)NSMutableArray * billarray;


@property (nonatomic,weak) WYCCardlistPickerView *PickerView;

@property (nonatomic,weak) UILabel *name;
@property (nonatomic,weak) UILabel *bank;

/** 金额*/
@property(nonatomic,strong)UITextField * moneyField;

@end

@implementation WYCFastCspController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"智能消费";
    
    self.view.backgroundColor = WYCGlobalBg;
    
    self.billarray = [[NSMutableArray alloc] init];
    
    //获取信用卡列表
    [self setupCreditcardlist];
    
    
    [self setupCreat];
}

- (void)setupCreditcardlist{
    NSString *cardlist = [NSString stringWithFormat:@"%@token=%@",creditcardlist,[WYCAccountTool unarchivetoken]];
    [HttpTool POST:cardlist params:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([[responseObject objectForKey:@"code"] intValue ] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                WYCBillModel * model = [WYCBillModel mj_objectWithKeyValues:dict];
                model.bank = [NSString stringWithFormat:@"%@(%@)",model.bankName,model.lastNumber];
                [self.billarray addObject:model];
            }
            
        }else{
            
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
            
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

- (void)setupCreat {
    /*** 姓名 ****/
    
    UIView * backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0, NavHeight, ScreenWidth, 360 * px);
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];
    
    //姓名
    UILabel * cardLabel = [[UILabel alloc] init];
    cardLabel.frame = CGRectMake(40 * px, WYCClearW, 500 * px, WYCClearH);
    cardLabel.text = @"信用卡号后四位:";
    cardLabel.font = MiddleLight;
    [backView addSubview:cardLabel];
    
    //赋值控件
    UILabel * card = [[UILabel alloc] init];
    card.frame = CGRectMake(CGRectGetMaxX(cardLabel.frame) + WYCClearW, cardLabel.y, ScreenWidth / 2, cardLabel.height);
    card.font = forLight;
    [backView addSubview:card];
    
    
    cardbutton = [[UIButton alloc] init];
    cardbutton.frame = CGRectMake(backView.width / 2, cardLabel.y, backView.width / 2 - 80 * px, cardLabel.height);
    
    cardbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [cardbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    cardbutton.titleLabel.font = [UIFont systemFontOfSize:WyzFont];
    [cardbutton addTarget:self action:@selector(cardButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:cardbutton];
    if ([_myFlag isEqualToString:@"333"]) {
        [cardbutton setTitle:@"请选择信用卡 >" forState:UIControlStateNormal];
        cardbutton.enabled =YES;
    }
    else{
        cardbutton.enabled =NO;
        
        NSString * Str = [NSString stringWithFormat:@"%@(%@)",_strBank,_strNumber];
        [cardbutton setTitle:Str forState:UIControlStateNormal];
    }
    
    UIView * cardview = [[UIView alloc] init];
    cardview.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(cardLabel.frame) + WYCClearW, ScreenWidth - 80 * px, 0.5);
    cardview.backgroundColor = WYCgrayColor;
    [backView addSubview:cardview];
    
    //姓名
    UILabel * moneyLabel = [[UILabel alloc] init];
    moneyLabel.frame = CGRectMake(cardLabel.x, CGRectGetMaxY(cardview.frame) + WYCClearW , cardLabel.width, WYCClearH);
    moneyLabel.text = @"消费金额:";
    moneyLabel.font = forLight;
    [backView addSubview:moneyLabel];
    
    
    //消费金额
    UITextField * moneyField = [[UITextField alloc] init];
    moneyField.frame = CGRectMake(CGRectGetMaxX(moneyLabel.frame), moneyLabel.y, backView.width - moneyLabel.width - 120 * px, moneyLabel.height);
    moneyField.textAlignment = NSTextAlignmentRight;
    moneyField.keyboardType = UIKeyboardTypeNumberPad;
    [IWNotificationCenter addObserver:self selector:@selector(textFields) name:UITextFieldTextDidChangeNotification object:moneyField];
    moneyField.font = forLight;
    [backView addSubview:moneyField];
    self.moneyField = moneyField;
    
    
    
    
    
    UIButton * implementButton = [[UIButton alloc] init];
    implementButton.frame = CGRectMake(40 * px,  240, ScreenWidth - 80 * px, 150 * px);
    implementButton.layer.cornerRadius = 10 * px;
    implementButton.layer.masksToBounds = YES;
    implementButton.backgroundColor =  WYCMain;
    [implementButton setTitle:@"下一步" forState:UIControlStateNormal];
    [implementButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [implementButton addTarget:self action:@selector(clickbtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:implementButton];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, implementButton.frame.size.height+implementButton.frame.origin.y+ 50 * px, ScreenWidth - 20, 155)];
    [label setTextColor:[UIColor redColor]];
    [label setText:@"备注：\n费率0.45%,每笔手续费：2元 \n(秒到账时间9：30-21：00) \n 交易限额： 500-20000元/笔，单卡/日5万 \n(另：建行、交行、招商单笔500-5000元)"];
    [label setFont:[UIFont systemFontOfSize:WyzFont]];
    label.textAlignment = NSTextAlignmentLeft;
    label.numberOfLines = 0;
    [self.view addSubview:label];
    backLabel = [[UILabel alloc] init];
    backLabel.textAlignment = NSTextAlignmentLeft;
    backLabel.font = [UIFont systemFontOfSize:WyzFont];
    backLabel.textColor = [UIColor grayColor];
    backLabel.numberOfLines = 0;

    backLabel.frame = CGRectMake(40 * px, backView.frame.size.height+backView.frame.origin.y+10, ScreenWidth - 80 * px, 108 * px);
    [self.view addSubview:backLabel];
}
- (void)textFields{
    WYCLogFunc;
    double a = [_moneyField.text doubleValue];
    
    double string = (a *0.0045)+2;
     backLabel.text= [NSString stringWithFormat:@"总计手续费：%.2f元",string];
}
- (void)cardButtonClick
{
    
    WYCLogFunc;
    if (!self.PickerView) {
        
        NSMutableArray *arr = self.billarray;
        WYCCardlistPickerView *PickerView = [[WYCCardlistPickerView alloc]initWithSourceData:arr confirmBlock:^(WYCBillModel * BillModel) {
            
            NSString * Str = [NSString stringWithFormat:@"%@(%@)",BillModel.bankName,BillModel.lastNumber];
            [cardbutton setTitle:Str forState:UIControlStateNormal];
            self.bank.text = BillModel.bankId;
            self.strCardid = BillModel.cardId;
            
            str = BillModel.cardId;
            [self pickerViewhide:YES];
            
            
        } cancelBlock:^(WYCBillModel * BillModel) {
            
            [self pickerViewhide:YES];
            
        }];
        PickerView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 220);
        [self.view addSubview:PickerView];
        self.PickerView = PickerView;
        [self pickerViewhide:NO];
    }
}

- (void)pickerViewhide:(BOOL)hide {
    
    if (hide) {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.PickerView.frame = CGRectMake(0, ScreenHeight, ScreenWidth, 280);
        }];
    }else {
        
        [UIView animateWithDuration:1 animations:^{
            
            self.PickerView.frame = CGRectMake(0, ScreenHeight - 280, ScreenWidth, 280);
        }];
    }
    
}

- (void)clickbtn:(UIButton *)btn{
    NSLog(@" %s ",__FUNCTION__);
    [[self class] cancelPreviousPerformRequestsWithTarget:self
                                                 selector:@selector(impleButtonClick)
                                                   object:btn];
    [self performSelector:@selector(impleButtonClick) withObject:btn afterDelay:1.0];
}
- (void)impleButtonClick{
    WYCLogFunc;
    [SVProgressHUD showWithStatus:@"加载中...."];
    if (self.moneyField.text.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请输入金额"];
        return;
    }
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"cardId"] = self.strCardid;
    params[@"amount"] = self.moneyField.text;
    params[@"token"] = [WYCAccountTool unarchivetoken];
    [HttpTool POST:creditConsume params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@=----------%@",params,responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [SVProgressHUD dismiss];

            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"成功"];
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }else{
            [SVProgressHUD dismiss];

            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:msgStr];
        }
        
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [SVProgressHUD dismiss];

        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:Failed];
    }];
    
    
}

//点击空白推荐盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.moneyField resignFirstResponder];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    //返回按钮
    UIButton * backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:backbutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0,titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"智能消费";
    [NavView addSubview:NavRigsterLabel];
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
@end
